// 03myCenter/02sectorization/03addressManage/addressManage.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var _this

Page({
   /* 页面的初始数据*/
   data: {
      //用户地址
      address: '',
      // 获取屏幕高度
      realHeight: '',
      //默认地址勾选 
      idx: '',
   },

   /*生命周期函数--监听页面加载*/
   onLoad: function (options) {
      _this = this
      var h = wx.getSystemInfoSync().windowHeight - 62
      this.setData({ realHeight: h })
      this.getUserAddress()
   },
   // 添加地址后刷新
   onShow: function(){
      this.getUserAddress()
   },
   //获取用户地址   
   getUserAddress() {
      var param = {
         User_ID: app.Data.userInfo.User_ID
      }
      co.getShareApi('user/Mg/address', param, function (res) {
         if (res.code == 0) {
            var d = []
            for( var i=0;i<res.data.length;i++ ){
               d.push(res.data[i].Address_Is_Default)
            }
            _this.setData({
               address: res.data,
               idx: d
            })
         }
      })
   },
   //点击勾选默认地址
   isCheck: function (e) {
      var idxx = e.currentTarget.id
      var d = this.data.idx
      for(var key in d){
         d[key]=0
      }
      d[idxx] = 1
      this.setData({ idx: d })
      var param = {
         User_ID: app.Data.userInfo.User_ID,
         Address_ID: e.currentTarget.dataset.value
      }
      co.getShareApi('user/Mg/setDefaultAddress',param,function(res){
         if(res.code == 0){
            co.Toast(res.msg)
         }      
      })
   },
   // 编辑地址
   editAddress: function (e) {
      var idx = e.currentTarget.id
      var AID = e.currentTarget.dataset.value
      var addressInfo = JSON.stringify(this.data.address[idx])
      wx.navigateTo({
         url: '/03myCenter/02sectorization/03addressManage/newOreditAddress/newOreditAddress?addressInfo=' + addressInfo + "&AddressIsDefault=" + AID,
      })
   },
   // 删除地址
   deleteAddress: function (e) {
      var Address_ID = e.currentTarget.id
      co.confirm('确认删除该地址吗？', '取消', '删除', function (res) {
         if (res) {
            var param = {
               User_ID: app.Data.userInfo.User_ID,
               Address_ID: Address_ID
            }
            co.getShareApi('user/Mg/deleteAddress', param, function (res) {
               _this.getUserAddress()
            })
         }
      })
   },

})